/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Block;

import DAO.AccountDAO;
import DAO.ConnectDatabase;
import DAO.DepartmentDAO;
import DAO.PatientDAO;
import Helper.DepartmentHelper;
import Helper.PatientHelper;
import Model.PatientModel;
import common.Constant;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author KIENLT
 */
public class Patient extends javax.swing.JPanel {

    ArrayList<PatientHelper> arrPatient;
    ArrayList<PatientHelper> arrPatientList;

    PatientHelper patientData;
    ResultSet rs = null;
    /**
     * Creates new form Patient
     */
    public Patient() {
        initComponents();
        changeLanguage();
        fillAllPatient();
        loadComboboxDepartment();
        loadComboboxAccount();
    }

    private void loadComboboxAccount() {
         try {
            DefaultComboBoxModel cbxAccont = new DefaultComboBoxModel();
            ResultSet rs = AccountDAO.FillAll(ConnectDatabase.ConnectionDatabase());
            while(rs.next()){
                String strId = rs.getString("AccountId");
                String strName = rs.getString("FullName");
                cbxAccont.addElement(new DepartmentHelper(strId, strName));
            }
            cbxAcount.setModel(cbxAccont);
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(rs);
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex1);
            }
            ex.printStackTrace();
        }
    }
    private void changeLanguage() {
        if(Constant.getLanguage() == 0){
            Constant.initBundle();
            getLanguage();
        }else{
            Constant.initBundle();
            getLanguage();
        }
    }
    private void loadComboboxDepartment(){
        try {
            DefaultComboBoxModel cbxAccontLevel = new DefaultComboBoxModel();
            ResultSet rs = DepartmentDAO.fillAllDepartment(ConnectDatabase.ConnectionDatabase());
            while(rs.next()){
                String strId = rs.getString("DepartmentId");
                String strName = rs.getString("DepartmentName");
                cbxAccontLevel.addElement(new DepartmentHelper(strId, strName));
            }
            cbxDep.setModel(cbxAccontLevel);
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(rs);
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex1);
            }
            ex.printStackTrace();
        }
    }
    
    private void getLanguage() {
        lblDesCription.setText(Constant.getBundle().getString("description"));
        lblAddress.setText(Constant.getBundle().getString("patientAddress"));
        lblAge.setText(Constant.getBundle().getString("age"));
        fullName.setText(Constant.getBundle().getString("patientName"));
        lblPatientID.setText(Constant.getBundle().getString("patientID"));
        lblGender.setText(Constant.getBundle().getString("patientGender"));
        lblPhone.setText(Constant.getBundle().getString("patientPhone"));
        
 //       btnAccept.setText(Constant.getBundle().getString("accept"));
        btnUpdate.setText(Constant.getBundle().getString("update"));
        btnup.setText(Constant.getBundle().getString("update"));
        btnAppoint.setText(Constant.getBundle().getString("appoint"));
        btnDelete.setText(Constant.getBundle().getString("delete"));
        btnAdd.setText(Constant.getBundle().getString("add"));
        btnSearch.setText(Constant.getBundle().getString("search"));
        
        lblDep.setText(Constant.getBundle().getString("department"));
        lblDocOld.setText(Constant.getBundle().getString("docOld"));
        lblDocNew.setText(Constant.getBundle().getString("docNew"));
        lblTimeBe.setText(Constant.getBundle().getString("timeBefore"));
        lblNextTime.setText(Constant.getBundle().getString("timeNext"));
        lblReasons.setText(Constant.getBundle().getString("reasons"));
              
    }

     private void fillAllPatient() {
        try {
            arrPatient = PatientDAO.FillAll(ConnectDatabase.ConnectionDatabase());
            arrPatientList = new ArrayList<PatientHelper>();

            for(PatientHelper patient : arrPatient){
                arrPatientList.add(patient);
            }
            tblPatient.setModel(new PatientModel(arrPatientList));
            
            if(arrPatientList.size() > 0){
                tblPatient.getSelectionModel().setSelectionInterval(0, 0);
                fillOnePatient();
            }
            
            tblPatient.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    
                    boolean adjust = e.getValueIsAdjusting();
                    if(adjust){
                        fillOnePatient();
                    }
                    
                }
            });
            
            tblPatient.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            tblPatient.getColumnModel().getColumn(0).setPreferredWidth(50);
            tblPatient.getColumnModel().getColumn(1).setPreferredWidth(150);
            tblPatient.getColumnModel().getColumn(2).setPreferredWidth(150);
            tblPatient.getColumnModel().getColumn(3).setPreferredWidth(50);
            tblPatient.getColumnModel().getColumn(4).setPreferredWidth(50);
            tblPatient.getColumnModel().getColumn(5).setPreferredWidth(80);
            tblPatient.getColumnModel().getColumn(6).setPreferredWidth(255);
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(Patient.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }

    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblPatient = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        lblPatientID = new javax.swing.JLabel();
        fullName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblGender = new javax.swing.JLabel();
        lblPhone = new javax.swing.JLabel();
        lblAge = new javax.swing.JLabel();
        lblDesCription = new javax.swing.JLabel();
        txtPatientID = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        txtPatienName = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        txtPhone = new javax.swing.JTextField();
        txtAge = new javax.swing.JTextField();
        cbxGender = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        btnAdd = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        cbxDep = new javax.swing.JComboBox();
        lblDep = new javax.swing.JLabel();
        lblDocOld = new javax.swing.JLabel();
        txtDoctorBefore = new javax.swing.JTextField();
        lblTimeBe = new javax.swing.JLabel();
        lblDocNew = new javax.swing.JLabel();
        dcBirthDay = new com.toedter.calendar.JDateChooser();
        lblNextTime = new javax.swing.JLabel();
        dcBirthDay1 = new com.toedter.calendar.JDateChooser();
        cbxAcount = new javax.swing.JComboBox();
        btnup = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtDescription1 = new javax.swing.JTextArea();
        lblReasons = new javax.swing.JLabel();
        btnAppoint = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(860, 560));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "List Patient", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        tblPatient.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane3.setViewportView(tblPatient);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 828, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Patient Infomation", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        lblPatientID.setText("PatientID");

        fullName.setText("Full Name");

        lblAddress.setText("Address");

        lblGender.setText("Gender");

        lblPhone.setText("Phone");

        lblAge.setText("Age");

        lblDesCription.setText("Description");

        txtPatientID.setEditable(false);
        txtPatientID.setEnabled(false);

        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        cbxGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Female", "Male" }));

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        jScrollPane2.setViewportView(txtDescription);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(243, 243, 243)
                .addComponent(btnAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSearch)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblAge, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblGender, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblAddress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblPatientID, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                    .addComponent(lblPhone, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                                .addComponent(txtPatientID, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtPhone)))
                        .addGap(66, 66, 66)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDesCription)
                            .addComponent(fullName)))
                    .addComponent(cbxGender, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtPatienName, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtAge, txtPatientID});

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAdd, btnDelete, btnSearch, btnUpdate});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPatientID)
                            .addComponent(txtPatientID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(fullName))
                    .addComponent(txtPatienName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblAddress)
                            .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDesCription))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblGender)
                            .addComponent(cbxGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPhone)
                            .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblAge)
                            .addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane2))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete)
                    .addComponent(btnSearch)
                    .addComponent(btnAdd)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Appointment", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        lblDep.setText("Department");

        lblDocOld.setText("Doctor old");

        lblTimeBe.setText("Time Before");

        lblDocNew.setText("Doctor new");

        lblNextTime.setText("Next Time");

        btnup.setText("Update");
        btnup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnupActionPerformed(evt);
            }
        });

        txtDescription1.setColumns(20);
        txtDescription1.setRows(5);
        jScrollPane4.setViewportView(txtDescription1);

        lblReasons.setText("Reasons");

        btnAppoint.setText("Appointment");
        btnAppoint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAppointActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblDocNew, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(btnAppoint)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnup, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(89, 89, 89))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(cbxAcount, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(lblNextTime, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(dcBirthDay1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblReasons, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblDep, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbxDep, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblDocOld, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDoctorBefore, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblTimeBe, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dcBirthDay, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cbxAcount, cbxDep});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(dcBirthDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblTimeBe, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cbxDep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblDep)
                        .addComponent(lblDocOld)
                        .addComponent(txtDoctorBefore, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lblDocNew)
                                        .addComponent(cbxAcount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(dcBirthDay1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(30, 30, 30))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblNextTime)
                                    .addComponent(lblReasons))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnup)
                                    .addComponent(btnAppoint))
                                .addGap(33, 33, 33))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        try {
            // TODO add your handling code here:
            PatientHelper pa = new PatientHelper();
            int row = tblPatient.getSelectedRow();
            pa.setPatientId(tblPatient.getValueAt(row, 0).toString());
            pa.setPatientName(txtPatienName.getText());
            pa.setPatientAddress(txtAddress.getText());
            //int cbx= Integer.parseInt(String.valueOf(cbxGender.getSelectedIndex()));
            //int dep=cbx+1;
            pa.setPatientGender(String.valueOf(cbxGender.getSelectedIndex()));
            pa.setPatientAge(txtAge.getText());
            pa.setPatientPhone(txtPhone.getText());
            pa.setPatientDescription(txtDescription.getText());

            if (!PatientDAO.checkPatient(ConnectDatabase.ConnectionDatabase(), pa)) {
                int query = PatientDAO.Insert(ConnectDatabase.ConnectionDatabase(), pa);
                if (query > 0) {
                    JOptionPane.showMessageDialog(this, "Insert Success.", "Message", JOptionPane.INFORMATION_MESSAGE);
                    fillAllPatient();
                } else {
                    JOptionPane.showMessageDialog(this, "Insert Fail.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(this, "UserName is exists.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(Rooms.class.getName()).log(Level.SEVERE, null, ex);
        }        // TODO add your handling code here:
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed

        try {
            // TODO add your handling code here:
            PatientHelper pa = new PatientHelper();
            int row = tblPatient.getSelectedRow();
            pa.setPatientId(tblPatient.getValueAt(row, 0).toString());
            pa.setPatientName(txtPatienName.getText());
            pa.setPatientAddress(txtAddress.getText());
            //int cbx= Integer.parseInt(String.valueOf(cbxGender.getSelectedIndex()));
            //int dep=cbx+1;
            pa.setPatientGender(String.valueOf(cbxGender.getSelectedIndex()));
            pa.setPatientAge(txtAge.getText());
            pa.setPatientPhone(txtPhone.getText());
            pa.setPatientDescription(txtDescription.getText());
            int query = PatientDAO.Update(ConnectDatabase.ConnectionDatabase(), pa);
            if(query > 0){
                JOptionPane.showMessageDialog(this, "Update Success.", "Message", JOptionPane.INFORMATION_MESSAGE);
                fillAllPatient();
            }else{
                JOptionPane.showMessageDialog(this, "Update Fail.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex);
        }// TODO add your handling code here:
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed

         try {
            PatientHelper pa = new PatientHelper();
            pa.setPatientId(txtPatientID.getText());
            int option = JOptionPane.showConfirmDialog(this, "You are sure delete?", "Message", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                int query = PatientDAO.Delete(ConnectDatabase.ConnectionDatabase(), pa);

                if (query > 0) {
                    JOptionPane.showMessageDialog(this, "Delete Success.", "Message", JOptionPane.INFORMATION_MESSAGE);
                    fillAllPatient();
                } else {
                    JOptionPane.showMessageDialog(this, "Delete Fail.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex);
        }// TODO add your handling code here:
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed

        try {
            PatientHelper pa = new PatientHelper();
            String seach="'%"+txtPatienName.getText()+"%'";
            pa.setPatientName(txtPatienName.getText());
            arrPatient = PatientDAO.SearchPa(ConnectDatabase.ConnectionDatabase(), pa);
            arrPatientList = new ArrayList<PatientHelper>();

            for(PatientHelper patient : arrPatient){
                arrPatientList.add(patient);
            }
            tblPatient.setModel(new PatientModel(arrPatientList));
            
            if(arrPatientList.size() > 0){
                tblPatient.getSelectionModel().setSelectionInterval(0, 0);
                fillOnePatient();
            }
            
            tblPatient.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
                    
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    
                    boolean adjust = e.getValueIsAdjusting();
                    if(adjust){
                        fillOnePatient();
                    }
                    
                }
            });
            
            tblPatient.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            tblPatient.getColumnModel().getColumn(0).setPreferredWidth(50);
            tblPatient.getColumnModel().getColumn(1).setPreferredWidth(150);
            tblPatient.getColumnModel().getColumn(2).setPreferredWidth(150);
            tblPatient.getColumnModel().getColumn(3).setPreferredWidth(50);
            tblPatient.getColumnModel().getColumn(4).setPreferredWidth(50);
            tblPatient.getColumnModel().getColumn(5).setPreferredWidth(80);
            tblPatient.getColumnModel().getColumn(6).setPreferredWidth(255);
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(Patient.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }// TODO add your handling code here:
    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnAppointActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAppointActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnAppointActionPerformed

    private void btnupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnupActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnupActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAppoint;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton btnup;
    private javax.swing.JComboBox cbxAcount;
    private javax.swing.JComboBox cbxDep;
    private javax.swing.JComboBox cbxGender;
    private com.toedter.calendar.JDateChooser dcBirthDay;
    private com.toedter.calendar.JDateChooser dcBirthDay1;
    private javax.swing.JLabel fullName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblAge;
    private javax.swing.JLabel lblDep;
    private javax.swing.JLabel lblDesCription;
    private javax.swing.JLabel lblDocNew;
    private javax.swing.JLabel lblDocOld;
    private javax.swing.JLabel lblGender;
    private javax.swing.JLabel lblNextTime;
    private javax.swing.JLabel lblPatientID;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblReasons;
    private javax.swing.JLabel lblTimeBe;
    private javax.swing.JTable tblPatient;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtAge;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextArea txtDescription1;
    private javax.swing.JTextField txtDoctorBefore;
    private javax.swing.JTextField txtPatienName;
    private javax.swing.JTextField txtPatientID;
    private javax.swing.JTextField txtPhone;
    // End of variables declaration//GEN-END:variables

    private void fillOnePatient() {
     
        try {
            patientData = new PatientHelper();
            int row = tblPatient.getSelectedRow();
            patientData.setPatientId(tblPatient.getValueAt(row, 0).toString());
            rs = PatientDAO.FillOne(ConnectDatabase.ConnectionDatabase(), patientData);
            if(rs.next()){
                txtPatientID.setText(rs.getString("PatientId"));
                txtPatienName.setText(rs.getString("PatientName"));
                txtAddress.setText(rs.getString("PatientAddress"));
                txtAge.setText(rs.getString("PatientAge"));
                txtPhone.setText(rs.getString("PatientPhone"));
                txtDescription.setText(rs.getString("PatientDescription"));
           //     int gender = Integer.parseInt(rs.getString("PatientGender"));
           //     cbxGender.setSelectedIndex(gender);
                int dep = Integer.parseInt(rs.getString("PatientGender"));
          
                cbxGender.setSelectedIndex(dep);
            }
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(rs);
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(Patient.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    
}
